// This program sorts a list of words in a text file into alphabetical order
#include <iostream>
#include <fstream>
#include <string>

const int MAX = 256;

using namespace std;

int stringCompare (const void* elem1, const void* elem2);

int main() {
	// 2 strings to hold input and output filenames
	string inFilename;
	string outFilename;

	// string array to hold our list, maximum of 256 items
	string list[MAX];

	// 2 files, Input - list to sort   Output - sorted list
	ifstream inFile;
	ofstream outFile;

	cout << "OMG SORT THAT SHIT -\n";

	// get the input and output filenames
	cout << "Input File: ";
	cin >> inFilename;
	cin.ignore();

	cout << "Output File: ";
	cin >> outFilename;
	cin.ignore();

	// INPUT
	inFile.open(inFilename.c_str());
	if (inFile.is_open()) {
		// get list from file
		for (int x = 0; x < MAX; x++) {
			if (!inFile.eof()) {
				inFile >> list[x];
			}
			else {
				break;
			}
		}
		inFile.close();

		// sort the array
		qsort(list, MAX, sizeof(string), stringCompare);
	}

	// Exit if input file is invalid
	else {
		cout << "Invalid input file.\n";
		cin.get();
		exit(1);
	}

	// OUTPUT
	outFile.open(outFilename.c_str(), ios::trunc);
	// if file is open, output object to file if object is not empty
	if (outFile.is_open()) {
		for(int x = 0; x < MAX; x++) {

			// if object is not empty...
			if(list[x] != "") {
				outFile << list[x] << "\n";
			}
		}
		outFile.close();
	}

}

int stringCompare (const void* elem1, const void* elem2) {
	return( strcmp(*(char **)elem1, *(char **)elem2) );
}
